<?php
    /*Scrive sulla pagina il menu principale del sito */
   
   ini_set('display_errors', true);
   require_once('model/db_driver.php');
   session_start();
   $utente = NULL;
   $mobile_phone = isMobilePhone(); // 0 se è un dispositvo desktop
   date_default_timezone_set('Europe/Rome');
   
   /*
    * Menu principale del sito
    * 
    */

	function printMasterMenu($pagina_selezionata='Home', $root='') {
		$pagine = array(1=>'Home',2=>'Rose',3=>'Classifiche', 4=>'Calendario',5=>'Bacheca',
		6=>'Dettagli partite',7=>'Voti A',8=>'Statistiche',9=>'Formazioni');
		$menu = "<table border='0' cellpadding='0' cellspacing='0' class='masterMenu'>";
		$menu = $menu .  loginModule($root);
		$menu = $menu .  "<tr >
						<td colspan='2' class='masterMenuTitleCell' style='z-index:-1;' >fanta leghe</td>
		</tr>\n";
		$menu = $menu .  "<tr><td colspan='2' style='width:100%;height:30px;vertical-align:bottom;white-space:nowrap;background-color:transparent;'>\n";
		$menu = $menu . "<ul style='list-style-type: none;display:inline;width:100%;background-color:transparent;'>\n";
		$count = count($pagine);
		$styles='';
	    for($i=1;$i<= $count; $i++){
	   		
			if($pagine[$i]==$pagina_selezionata){
				$styles="class='masterMenuCell masterMenuCellSel'";
			}else{
				$styles="class='masterMenuCell'";
	   		}
			$linkPage = strtolower($root . str_replace(" ", "_", $pagine[$i])) . ".php";
			$menu = $menu .  "<li $styles>";
			$menu = $menu .  "<a style='color:#00FF21; font-weight:bold;text-decoration:none;' href='$linkPage'>";
			$menu = $menu .  "$pagine[$i]</a></li>\n";
	   			
   		}
		//se l'utente è connesso mostra il menu per la modifica della formazione
		if(User::getCurrentUser()!=null){
			if($pagina_selezionata=='Modifica formazione'){
				$menu = $menu .  "<li class='masterMenuCell masterMenuCellSel'>";
			}else{
				$menu = $menu .  "<li class='masterMenuCell'>";	
			}
			$menu = $menu .  "<a style='color:#00FF21; font-weight:bold;text-decoration:none;' href=" . $root . "modifica_formazione.php>";
			$menu = $menu .  "Modifica formazione</a></li>\n";
			
			//se l'utente è un amministratore visualizza la pagina di amministrazione
			if(User::getCurrentUser()->getRuolo()>=User::$ADMIN){
				if($pagina_selezionata=='Admin'){
					$menu = $menu .  "<li class='masterMenuCell masterMenuCellSel'>";
					$menu = $menu .  "<span style='color:black;text-decoration:none;'>";
					$menu = $menu .  "Admin</span></li>\n";
				}else{
					$menu = $menu .  "<li class='masterMenuCell'>";
					$menu = $menu .  "<a style='color:#00FF21; font-weight:bold;text-decoration:none;' href='admin/admin.php'>";
					$menu = $menu .  "Admin</a></li>\n";
				}

			} 
		}
	   $menu = $menu .  "</ul></td></tr></table>\n";
	   return $menu;
   }
	
	function loginModule($root){
		global $utente;
		$module= "<tr>
					<td style='background-color:rgb(150,202,150);'>&nbsp;</td>
					<td style='width:750px;text-align:right;'>
						<table style='width:100%;background-color:rgb(150,202,150);font-size:10px;'><tr>";
		$logged = false;
		If(isset($_POST['EndSession'])){
			User::setCurrentUser(null);
			header("location:" . $root . "home.php");
			exit();
			
		}

		if(($utente=User::getCurrentUser())==null){
			if(isset($_POST['txtUtente'])){
				$user=$_POST['txtUtente'];
				$pwd=$_POST['txtPassword'];
				$driver = new DbDriver();
				$utente = $driver->db_getUser($user, $pwd);
				if($utente != NULL){
					User::setCurrentUser($utente);
					$logged = true;	
				}else{
					$module = $module . "<td style='color:red;text-align:right;width:100%;font-size:12px;'>password errata. Si prega di riprovare</td></tr><tr>";
				}					
			}else{
				$logged = false;
			}

		}else{
			$logged = true;
		}
		
		
		
		if($utente!=null){
			//$utente = $_SESSION['user'];
			$user = $utente->getAlias();
			$module = $module . "
				<td style='width:80%'>&nbsp;</td>
				<td style='width:20%;text-align:right;vertical-align:middle;'>
					<div style='color:blue;margin-right:10px;white-space:nowrap;padding-bottom:5px;'>bentornato $user</div>
				</td>
				<td style='width:20%;text-align:right;'>
					<form method='GET' action='./chg_pwd.php'>
						<input type='submit' style='font-size:11px;' name='btnChangePwd' value='Cambia password' />
					</form>
				</td>
				<td style='width:10%;text-align:right;'>
					<form method='POST' action='?'>
						<input type='submit' style='font-size:11px;' name='EndSession' value='Esci' />
					</form>								
				</td>
				";
		}
					
		
		if(!$logged){
			$module= $module .  "
			<td style='width:100%;text-align:right;'>
				<form method='POST' >
					Nome utente: <input style='font-size:11px;width:80px;' type='text' value='' name='txtUtente'>&nbsp;&nbsp;
					Password: <input style='font-size:11px;width:80px;' type='password' value='' name='txtPassword'>&nbsp;&nbsp;
					<input style='font-size:10px;' type='submit' value='Accedi'>
				</form>
			</td>";
		}
		//echo var_dump($_POST);
		$module= $module .  "</tr></table>\n";
		return $module;
	}
	
	function getMenuLeghe($page="", $lega_id=-1, &$cur_lega=null){
	/* stampa la lista delle leghe*/
		
		$nome_lega='';
		$id=0;
		$leghe = Lega::caricaLeghe();
		$html = 
		"
		<div class='menu_leghe' >
		<span style='color:#0A246A;font-weight:bold;text-align:center;margin-left:60px'>Leghe:</span>
		<table cellpadding='0' cellspacing='0' style='width:100%'>"; //background-color:#E0EFFF;
		for($i=0;$i<count($leghe); $i++){
			if(!$leghe[$i]->isAttiva()) continue;
			$nome_lega = $leghe[$i]->getNome();
			$id = $leghe[$i]->getLegaId();
			$class_a = "";
			if($lega_id==$leghe[$i]->getLegaId()){
				$cur_lega = $leghe[$i];
				$class_a = "lega_sel";
			}
			$alt=1; /*$i%2; non faccio più vedere qui stili alternati */
			$html = $html . "
			<tr>
				<td class='rowLega_$alt'><a class='lega $class_a' href='" . $page . "l=$id'>$nome_lega</a></td>
			</tr>";
		}
		return $html . "
			<tr>
				<td class='lastRowLega'>&nbsp;</td>
			</tr>
		</table>
		</div>";
		
	}

	function printFooter($pageName=''){
		echo "
			<!-- <script type='text/javascript'>
			/* <![CDATA[ */
			//document.write('<s'+'cript type=\"text/javascript\" src=\"http://ad.altervista.org/js.ad/size=728X90/r='+new Date().getTime()+'\"><\/s'+'cript>');
			/* ]]> */
			</script> -->
			<br />
			<div style='width:100%;text-align:left;font-size:12px;background-color:gray;color:white;'>
				Sito ideato e creato da <a style='color:white;font-size:12px;' href='mailto:cangemi.daniele@gmail.com'>Daniele Cangemi</a>
			</div>
		";
	}
	
	/*
	 * Controlla se lo user agent appartiene ad un dispositvo mobile
	 * */
	 function isMobilePhone(){
		 
		$mobile_browser = '0';
		 
		if (preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone|android)/i', strtolower($_SERVER['HTTP_USER_AGENT']))) {
		    $mobile_browser++;
		}
		 
		if ((strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml') > 0) or ((isset($_SERVER['HTTP_X_WAP_PROFILE']) or isset($_SERVER['HTTP_PROFILE'])))) {
		    $mobile_browser++;
		}    
		 
		$mobile_ua = strtolower(substr($_SERVER['HTTP_USER_AGENT'], 0, 4));
		$mobile_agents = array(
		    'w3c ','acs-','alav','alca','amoi','audi','avan','benq','bird','blac',
		    'blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno',
		    'ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-',
		    'maui','maxo','midp','mits','mmef','mobi','mot-','moto','mwbp','nec-',
		    'newt','noki','oper','palm','pana','pant','phil','play','port','prox',
		    'qwap','sage','sams','sany','sch-','sec-','send','seri','sgh-','shar',
		    'sie-','siem','smal','smar','sony','sph-','symb','t-mo','teli','tim-',
		    'tosh','tsm-','upg1','upsi','vk-v','voda','wap-','wapa','wapi','wapp',
		    'wapr','webc','winw','winw','xda ','xda-');
		 
		if (in_array($mobile_ua,$mobile_agents)) {
		    $mobile_browser++;
		}
		 
		if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']),' ppc;') > 0) {
		    $mobile_browser++;
		}
		
		if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']),'iemobile')>0) {
			$mobile_browser++;
		}
		 
		if (strpos(strtolower($_SERVER['HTTP_USER_AGENT']),'windows') > 0) {
		    $mobile_browser = 0;
		}
		 
		return $mobile_browser;
		 
	}


?>